﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MtApi;

namespace MTEA.LIB.Analysis.KDJ
{
    public enum KDJCrossType
    {
        None,
        Up,
        Down
    }

    public class KDJCrossResult : IComparable
    {
        public KDJCrossType CrossTye { get; set; }
        public double Value { get; set; }
        public DateTime Time { get; set; }

        public int CompareTo(object obj)
        {
            var other = obj as KDJCrossResult;
            return this.Time.CompareTo(other.Time);
        }
    }
    public static class KDJAnalysis
    {

        public static KDJCrossResult getKDJCross(this MtApiClient client, string symbol, int timeframes,int shift1,int shift2)
        {
            double prevMain = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 0, shift2);
            double prevSignal = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 1, shift2);
            double main = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 0, shift1);
            double signal = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 1, shift1);

            var crossPoint = BaseAnalysis.CrossPoint(0, prevMain, 1, main, 0, prevSignal, 1, signal);
            var result=new KDJCrossResult() {Time = DateTime.Now};
            if (crossPoint.Item1 >1)
            {
                result.CrossTye = KDJCrossType.None;
                result.Value = double.MinValue;
                return result;
            }
            result.Value = crossPoint.Item2;
            if (signal > prevSignal)
            {
                result.CrossTye=KDJCrossType.Up;
            }
            else
            {
                result.CrossTye = KDJCrossType.Down;
            }
            return result;

        }

        public static double getKDJMain(this MtApiClient client, string symbol, int timeframes,int shift)
        {
            return client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 0, shift);
        }
        public static double getKDJSignal(this MtApiClient client, string symbol, int timeframes,int shift)
        {
            return client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 1, shift);
        }


        public static KDJResult getKDJResult(this MtApiClient client,string symbol, int timeframes)
        {
            double prevMain = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 0, 4);
            double prevSignal = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 1, 4);
            double main = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 0, 2);
            double signal = client.iStochastic(symbol, timeframes, 9, 3, 3, 0, 0, 1, 2);
            //MTLogcat.d(Tag, "Checking KDJ isValid ");
            //MTLogcat.d(Tag, string.Format("                 main: {0} signal : {1}", main, signal));
            //MTLogcat.d(Tag, string.Format("                 prevMain: {0} prevSignal : {1}", prevMain, prevSignal));
            //if (main > signal && prevMain < prevSignal && main < 25 && signal < 25 && prevMain < 25 && prevSignal < 25)
            //{
            //    //buy
            //    return KDJResult.Buy;
            //}

            if (main < 15 && signal < 15)
            {
                //buy
                return KDJResult.Buy;
            }

            // else if (main < signal && prevMain > prevSignal && main >75 && signal > 75 && prevMain >75 && prevSignal >75 )
            else if (main > 90 && signal > 90)
            {
                //sell
                return KDJResult.Sell;
            }
            return KDJResult.None;
        }

    }
}
